package com.yonyou.brigade.facade.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yonyou.brigade.order_center.model.*;
import com.yonyou.brigade.order_center.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/orderCenter")
@RestController
public class OrderCenterController {

    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("briFlightOrderServiceImpl")
    private IBriFlightOrderService briFlightOrderService;

    @Autowired
    @Qualifier("briFlightInfoServiceImpl")
    private IBriFlightInfoService flightInfoService;

    @Autowired
    @Qualifier("briFlightPassengerServiceImpl")
    private IBriFlightPassengerService passengerService;

    @Autowired
    @Qualifier("briFlightOrderContactServiceImpl")
    private IBriFlightOrderContactService orderContactService;

    @Autowired
    @Qualifier("briOrderServiceImpl")
    private IBriOrderService briOrderService;

    /**
     * 将订单数据按一定格式组装数据
     * @param params
     * @return
     */
    @RequestMapping(value = "/getFlightData", method = RequestMethod.POST)
    public JSONArray getFlightData(@RequestBody Map params) {
        JSONArray result = new JSONArray();
        String fromType = String.valueOf(params.get("fromType"));
        EntityWrapper<BriFlightOrder> briFlightOrderEntityWrapper = new EntityWrapper<>();
        briFlightOrderEntityWrapper.where("from_type={0}", fromType);
        List<BriFlightOrder> briFlightOrders = briFlightOrderService.selectList(briFlightOrderEntityWrapper);
        if (!CollectionUtils.isEmpty(briFlightOrders)) {
            for (BriFlightOrder briFlightOrder : briFlightOrders) {
                JSONObject jsonObject = new JSONObject();
                String orderNo = briFlightOrder.getOrderNo();
                JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(briFlightOrder));

                EntityWrapper<BriFlightInfo> briFlightInfoEntityWrapper = new EntityWrapper<>();
                briFlightInfoEntityWrapper.where("order_no={0}", orderNo);
                List<BriFlightInfo> flightInfos = flightInfoService.selectList(briFlightInfoEntityWrapper);
                jsonObject1.put("flightInfo", flightInfos);

                EntityWrapper<BriFlightPassenger> briFlightPassengerEntityWrapper = new EntityWrapper<>();
                briFlightPassengerEntityWrapper.where("order_no={0}", orderNo);
                List<BriFlightPassenger> flightPassengers = passengerService.selectList(briFlightPassengerEntityWrapper);
                jsonObject1.put("flightPassenger", flightPassengers);

                EntityWrapper<BriFlightOrderContact> briFlightOrderContactEntityWrapper = new EntityWrapper<>();
                briFlightOrderContactEntityWrapper.where("order_no={0}", orderNo);
                List<BriFlightOrderContact> flightOrderContacts = orderContactService.selectList(briFlightOrderContactEntityWrapper);
                jsonObject1.put("flightContact", flightOrderContacts);

                result.add(jsonObject1);
            }
        }
        return result;
    }

    /**
     * 获取个人订单列表的筛选条件
     * @param params
     * @return
     */
    @RequestMapping(value = "/query/personal/menuOptions", method = RequestMethod.POST)
    public Map getMenuOptions(@RequestBody Map params) {
        return briOrderService.getPersonalMenuOptions(params);
    }

    /**
     * 获取个人订单列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/query/personal/orderLists", method = RequestMethod.POST)
    public List<BriOrderListVO> getOrderInfos(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        logger.info("searching for personal orderLists' params: " + JSON.toJSONString(params));
        return briOrderService.getBriOrderList(params);
    }

    /**
     * 获取个人订单详情
     * @param params
     * @return
     */
    @RequestMapping(value = "/query/personal/orderDetail", method = RequestMethod.POST)
    public JSONObject getBriOrderDetail(@RequestBody JSONObject params) {
        logger.info("searching for personal orderDetail' params: " + JSON.toJSONString(params));
        return briOrderService.getBriOrderDetail(params);
    }

    /**
     * 根据申请单号更新订单中心中的申请单信息
     * @param params
     * @return
     */
    @RequestMapping(value = "/update/aboutSQ", method = RequestMethod.POST)
    public String updateAboutSQ(@RequestBody JSONObject params) {
        if ("135246".equals(params.getString("token"))) {
            JSONArray relateYBZIds = params.getJSONArray("relateYBZIds");
            String tenantId = params.getString("tenantId");
            if (!CollectionUtils.isEmpty(relateYBZIds)) {
                briOrderService.updateSQByRelateYbzIds(relateYBZIds, tenantId);
            }
        }
        return "更新成功！";
    }

    /**
     * excel导入订单数据到订单中心
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/api/v1/import/importOrderList", method = RequestMethod.POST)
    public JSONObject importOrderList(HttpServletRequest request) throws Exception {
        String tenantId = request.getParameter("tenantId");
        String type = request.getParameter("type");
        if (StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(type)) {
            throw new Exception("tenantId 或 type 为空");
        }
        MultipartFile file = ((DefaultMultipartHttpServletRequest) request).getFile("file");
        JSONObject result = briOrderService.importOrderList(tenantId, type, file);
        return result;
    }
}
